#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/named_bl.decommissioned,v 1.1 2012/11/27 07:50:51 yajin Exp $
#

# Author: yang.jin@ebay.com


print_usage()
{

echo "
This tools is to redirect certain Internet addresses in IT DNS servers in order to block access to them.

Usage:
         named_bl -a \"domain name(s)\" \"SR#(comment)\"
         named_bl -d \"domain name(s)\" \"SR#(comment)\"
         named_bl -A file-name \"SR#(comment)\"
         named_bl -D file-name \"SR#(comment)\"

Options:
	-a: blacklist domains delimited by a blank space.
	-d: remove blacklisted domains delimited by a blank space.
	-A: blacklist domains on a file.
	-D: remove blacklisted domains on a file.

"
}

#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
add_domain()
{

tmp1=/tmp/bl.zones1.$$
cp -p $BL_CONFIG $tmp1

for bldomain in $domains; do

  $GREP -q "^[ 	]*zone[ 	]\{1,\}\"${bldomain}\"" $BL_CONFIG
  if [ $? -ne 0 ]; then
  	echo -e "
zone \"$bldomain\" { // $comment
	type master;
  	file \"bl/db.${bldomain}\";
};" >> $tmp1
  fi

  $SED "s/BLACKLISTED_DOMAIN/$bldomain/g" $BL_DB_TEMPLATE > ${BL_DB_PATH}/db.${bldomain}

  msg=$($CHKZONE $bldomain ${BL_DB_PATH}/db.${bldomain})
  echo $msg | $GREP -q " OK$"
  if [ $? -ne 0 ]; then
	echo $msg >&2
	exit 2
  fi

done

$CHKCONF $tmp1
if [ $? -ne 0 ]; then 
	echo -e "The temporary file \n$tmp1 for $BL_CONFIG failed to pass syntax checking, exited." >&2
	exit 1
fi


echo -e "Updating blacklisted domains on $(hostname)... \c"

cd $(dirname $BL_CONFIG)
co -l $(basename $BL_CONFIG) 2>/dev/null
cp  $tmp1 $BL_CONFIG
# comment=$(echo $comment | $SED "s/^#/Request #/")
ci -m"$comment" -u $(basename $BL_CONFIG) 2>/dev/null

rm -f  $tmp1
$RNDC reconfig

echo "Done."


# copy bl.zonee and db.$bldomain to all the other dns servers
for server in $DNS_PSEUDO_MASTERS $DNS_SLAVES; do 
	echo -e "Updating blacklisted domains on $server... \c"

	$RSYNC $BL_CONFIG ${server}:${BL_CONFIG}
	$RSYNC -a $BL_DB_PATH ${server}:$(dirname $BL_DB_PATH)
	ssh $server "$RNDC reconfig"

	echo "Done."
done

}

#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
delete_domain()
{

tmp1=/tmp/bl.zones1.$$
tmp2=/tmp/bl.zones2.$$

cp -p $BL_CONFIG $tmp1

for bldomain in $domains; do 
	$SED "/^[[:space:]]*zone[[:space:]]\{1,\}\"$bldomain\"/,/^[[:space:]]*\}\;/d" $tmp1 > $tmp2
	tmp=$tmp1; tmp1=$tmp2; tmp2=$tmp
done

$CHKCONF $tmp1
if [ $? -ne 0 ]; then 
	echo -e "The temporary file \n$tmp1 for $BL_CONFIG failed to pass syntax checking, exited." >&2
	exit 1
fi


echo -e "Updating blacklisted domains on $(hostname)... \c"

for bldomain in $domains; do 
	rm -f ${BL_DB_PATH}/db.${bldomain}
done

cd $(dirname $BL_CONFIG)
co -l $(basename $BL_CONFIG) 2>/dev/null
cp  $tmp1 $BL_CONFIG
# comment=$(echo $comment | $SED "s/^#/Request #/")
ci -m"$comment" -u $(basename $BL_CONFIG) 2>/dev/null

rm -f  $tmp1 $tmp2
$RNDC reload

echo "Done."


# copy bl.zonee and db.$bldomain to all the other dns servers
for server in $DNS_PSEUDO_MASTERS $DNS_SLAVES; do 
	echo -e "Updating blacklisted domains on $server... \c"

	$RSYNC $BL_CONFIG ${server}:${BL_CONFIG}
#	$RSYNC -a $BL_DB_PATH ${server}:$(dirname $BL_DB_PATH)
	ssh $server "$RNDC reload"

	echo "Done."
done
}

#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------

export PATH=/bin:/usr/bin:/usr/local/bin

# ROOT="/MINE"
ROOT=""
RNDC="/opt/bind/sbin/rndc"

RSYNC="/usr/local/bin/rsync"
CHKCONF="/opt/bind/sbin/named-checkconf"
CHKZONE="/opt/bind/sbin/named-checkzone"
GREP="/usr/xpg4/bin/grep"
SED="/usr/xpg4/bin/sed"
ISPRI="$ROOT/usr/local/sbin/is_primaster"

BL_CONFIG="$ROOT/etc/named/bl.zones"
BL_DB_PATH="$ROOT/var/named/db/bl"
BL_DB_TEMPLATE="$ROOT/etc/named/BLACKLISTED_DOMAIN_TEMPLATE"

DNS_SERVERS_LIST="$ROOT/etc/named/IT_DNS_SERVERS"


if [ ! -f $DNS_SERVERS_LIST ]; then
	echo -e "The configuration file \"$DNS_SERVERS_LIST\" does not exist." >&2
	exit 2
fi

. $DNS_SERVERS_LIST

if [ ! -x $ISPRI ]; then
	echo "$ISPRI does not exist, or, is not executable." >&2
        exit 2
fi

$ISPRI
if [ $? -ne 0 ]; then 
	echo "This host is not the primary DNS master."  >&2
	exit 2
fi

DATE=$(/usr/xpg4/bin/date "+%C%y%m%d%H%M")

if [ ! -f $BL_DB_TEMPLATE ]; then
	echo -e "The blacklisted domain template \"$BL_DB_TEMPLATE\" does not exist." >&2
	exit 2
fi

ops=""
domain=""
file=""

while [ "X$1" != "X" ]; do
  case $1 in 
	'-a')
		ops="adddomain"; 
		shift
		domains=$1
		shift
	;;
	'-d')
		ops="deletedomain"; 
		shift
		domains=$1
		shift
	;;
	'-A')
		ops="addfile"; 
		shift
		file=$1
		shift
	;;
	'-D')
		ops="deletefile"; 
		shift
		file=$1
		shift
	;;
	*)
		if [ "X$ops" != "X" ]; then comment=$1; fi
		shift
	;;
  esac
done

if [ "X$ops" == "X" -o "X$comment" == "X" -o "X$domains" == "X" -a "X$file" == "X" ]; then
	print_usage; exit 1
fi


umask 022

# echo -e "Backing up $BL_CONFIG as $BL_CONFIG.${DATE}... \c"
# cp -p $BL_CONFIG $BL_CONFIG.${DATE}
# echo "Done"

case $ops in
  'adddomain')
	if [ "X$comment" == "X" ]; then
		print_usage
		exit 1
	fi
	add_domain
	;;

  'addfile')
	if [ "X$file" == "X" ]; then
		echo  -e "File $file doesn\'t exist."
		print_usage
		exit 1
	fi
	if [ "X$comment" == "X" ]; then
		print_usage
		exit 1
	fi
	domains=$($SED "s/#.*//" $file)
	add_domain
	;;

  'deletedomain')
	delete_domain
	;;

  'deletefile')
	if [ "X$file" == "X" ]; then
		echo  -e "File $file doesn\'t exist."
		print_usage
		exit 1
	fi
	domains=$($SED "s/#.*//" $file)
	delete_domain
	;;

  *)
	print_usage
	;;
esac


